# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import re


def getDict(farg=None, **kwargs):
    # print "mk a dict:%s"%farg
    d = {}
    for key in kwargs:
        # print "key: %s,value: %s"%(key,kwargs[key])
        d[key] = kwargs[key]
    return d


def get_format_date(date, separator='-', format=None):
    # param date :"2017-03-25"
    ##param separator :'-','/'
    ##param format :YYYY--MM--DD to do
    # check whether the date is right
    # YYYY--MM--DD
    # 闰年匹配："2016-2-29" only the day:02-29
    # year_run=r"(?P<year>(([0-9]{2})(0[48]|[2468][048]|[13579][26]))|((0[048]|[2468][048]|[13579][26])00))-(?P<month>02)-(?P<day>29)"
    # 平年匹配："2017-2-23"
    # year_ping=r"([0-9]{4})-(((0[13578]|1[02])-(0[1-9]|1[0-9]|2[0-9]|3[01]))|((0[469]|11)-(0[1-9]|1[0-9]|2[0-9]|30))|((02)-(0[1-9]|1[0-9]|2[0-8])))"
    # use both:

    re_year_check = r"((?P<year>(([0-9]{2})(0[48]|[2468][048]|[13579][26]))|((0[048]|[2468][048]|[13579][26])00))-(?P<month>02)-(?P<day>29))|(([0-9]{4})-(((0[13578]|1[02])-(0[1-9]|1[0-9]|2[0-9]|3[01]))|((0[469]|11)-(0[1-9]|1[0-9]|2[0-9]|30))|((02)-(0[1-9]|1[0-9]|2[0-8]))))"
    result = re.match(re_year_check, date)
    if result is None:
        print("the date does not exist!")
        return 0
    else:
        newdate = re.sub(r"[-/\_]", separator, date)
        return newdate


###################################################
def fun_var_args(farg, *args):
    print("arg:", farg)
    for value in args:
        print("another arg:", value)


''' result1
arg: 1  
another arg: two  
another arg: 3 
'''


def fun_var_kwargs(farg, **kwargs):
    print("arg:", farg)
    for key in kwargs:
        print("another keyword arg: %s: %s" % (key, kwargs[key]))


''' result2  
arg: 1  
another keyword arg: myarg2: two  
another keyword arg: myarg3: 3 
'''
################################################### 

if __name__ == '__main__':
    fun_var_kwargs(farg=1, myarg2="two", myarg3=3)  # myarg2和myarg3被视为key， 感觉**kwargs可以当作容纳多个key和value的dictionary
    fun_var_args(1, "two", 3)  # *args可以当作可容纳多个变量组成的list
    # date_OBV_list=[]
    # date_OBV_list.append(getDict(date='2018',OBV=0.0))
    # print date_OBV_list
    #######################
    '''Test use re to check date
  
    year_run=r"(?P<year>(([0-9]{2})(0[48]|[2468][048]|[13579][26]))|((0[048]|[2468][048]|[13579][26])00))-(?P<month>02)-(?P<day>29)"
    year_ping=r"([0-9]{4})-(((0[13578]|1[02])-(0[1-9]|1[0-9]|2[0-9]|3[01]))|((0[469]|11)-(0[1-9]|1[0-9]|2[0-9]|30))|((02)-(0[1-9]|1[0-9]|2[0-8])))"
    year_both=r"((?P<year>(([0-9]{2})(0[48]|[2468][048]|[13579][26]))|((0[048]|[2468][048]|[13579][26])00))-(?P<month>02)-(?P<day>29))|(([0-9]{4})-(((0[13578]|1[02])-(0[1-9]|1[0-9]|2[0-9]|3[01]))|((0[469]|11)-(0[1-9]|1[0-9]|2[0-9]|30))|((02)-(0[1-9]|1[0-9]|2[0-8]))))"
    s1=re.match(r1,'2016-02-29')
    s2=re.match(r3,'0000-02-29')
    if s2 is None:
        print "2"

    t=get_format_date("2017-03-29","/")
    print t   
    t_wrong=get_format_date("2017-3-29","/")
    print t 
    '''
    #######################
